package com.shadow.service;

import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.path.EntityPathBase;
import com.shadow.repository.MyCustomRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public abstract class EntityService<T, R extends MyCustomRepository<T, Long>> {
    protected R repo;

    protected EntityService(R repo) {
        this.repo = repo;
    }

    public Page<T> findPage(Pageable pageable) {
        return repo.findAll(pageable);
    }

    public T save(T entity) {
        return repo.save(entity);
    }

    public List<T> save(List<T> entities) {
        return repo.save(entities);
    }

    public void delete(Long id) {
        repo.delete(id);
    }

    public void delete(T entity) {
        repo.delete(entity);
    }

    public void delete(List<T> list) {
        repo.delete(list);
    }

    public List<T> query(EntityPathBase<T> entityToSelect, Predicate... o) {
        return repo.queryDsl(entityToSelect, o);
    }

    public T querySingle(EntityPathBase<T> entityToSelect, Predicate... o) {
        return repo.querySingle(entityToSelect, o);
    }

    public List<T> queryWithOrderAndLimit(EntityPathBase<T> entityToSelect,
                                          OrderSpecifier<?> order, int limit, Predicate... o) {
        return repo.queryDslWithOrderAndLimit(entityToSelect, order, limit, o);
    }


    public EntityPathBase<T> GetEntityPathBase() {
        return null;
    }

    public List<T> queryDslWithOrder(EntityPathBase<T> entityToSelect,
                                     Predicate o, OrderSpecifier<?> order) {
        return repo.queryDslWithOrder(entityToSelect, order, o);
    }

    public T findOne(Long id) {
        return repo.findOne(id);
    }

    public List<T> findAll() {
        return repo.findAll();
    }

}

